package com.day240701.util;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;

public class JDBCDemo05 {
    public static void main(String[] args) throws Exception {
        Class.forName("com.mysql.cj.jdbc.Driver");
        Connection conn  =  DriverManager.getConnection("jdbc:mysql://localhost:3306/demo","root","root");
        // 插入
        /*PreparedStatement psm = conn.prepareStatement("insert into t_student(sname, password, sex, age) values(?,?,?,?)");
        psm.setString(1, "刘德华");
        psm.setString(2, "123");
        psm.setString(3, "男");
        psm.setInt(4, 50);

        int count = psm.executeUpdate();
        System.out.println(count > 0 ? "添加成功" : "添加失败");*/

        // 修改
        /*PreparedStatement psm = conn.prepareStatement("update t_student set sname=?, password=?, sex=?, age=? where sno=?");
        psm.setString(1, "张曼玉");
        psm.setString(2, "123");
        psm.setString(3, "女");
        psm.setInt(4, 40);
        psm.setInt(5, 1009);

        int count = psm.executeUpdate();
        System.out.println(count > 0 ? "添加成功" : "添加失败");*/

        // 删除
        PreparedStatement psm = conn.prepareStatement("delete from t_student where sno=?");
        psm.setInt(1, 1010);

        int count   = psm.executeUpdate();
        System.out.println(count>0?"删除成功":"删除失败");

        psm.close();
        conn.close();
    }
}
